<?php
defined('BASEPATH') or exit('No direct script access allowed');

class MonitorVariety extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('Monitor_variety_model'); // 假设有一个名为Example_model的模型

		$this->load->library('stringutil');

		header("Access-Control-Allow-Origin: *");
		header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
		header("Access-Control-Allow-Headers: Content-Type, Authorization");
	}

	public function handle()
	{
		// $a = new stdClass();
		// $a->id = "";
		// $a->platform = "dce";
		// $a->variety_code = "a";
		// $a->master_date = "2503";
		// $a->slave_platform = "dce";
		// $a->slave_variety_code = "a";
		// $a->slave_date = "2505";
		// $a->wave = "30";

		// $array = array($a);
		// $b = json_encode($array);

		$result = new stdClass();
		$result->is_success = false;
		$result->error_messages = "";

		// 检查参数格式
		$datas = $this->input->post('datas');
		$datas = json_decode($datas);
		if (!is_array($datas)) {
			$result->error_messages = "datas 不是一个 Json 字符串。";
			log_message("error", "handle, datas 不是一个 Json 字符串。" . json_encode($datas));
			return $this->output
				->set_content_type('application/json')
				->set_output(json_encode($result));
		}
		
		$delete_ids = $this->input->post('delete_ids');
		if ($delete_ids) {
			$delete_ids = json_decode($delete_ids);
			if (!is_array($delete_ids)) {
				$result->error_messages = "delete_ids 不是一个 Json 字符串。";
				log_message("error", "handle, delete_ids 不是一个 Json 字符串。" . json_encode($delete_ids));
				return $this->output
					->set_content_type('application/json')
					->set_output(json_encode($result));
			}
		}

		// 拼装数据
		$insert_monitor_list = array();
		$update_monitor_list = array();

		foreach ($datas as $data) {

			// 处理 expiration_date
			$master_date = $data->master_date;
			$slave_date = $data->slave_date;

			$expiration_date = $master_date > $slave_date ? $slave_date : $master_date;
			if (strlen($expiration_date) == 3) {
				$expiration_date = "2" . $expiration_date;
			}

			$year = "20" . substr($expiration_date, 0, 2);
			$month = substr($expiration_date, 2, 2);
			$date = new DateTime($year . "-" . $month . "-01");
			$expiration_date = $date->format('Y-m-d');

			// 处理 variety_code slave_variety_code
			$variety_code = $data->variety_code;
			$slave_variety_code = $data->slave_variety_code;
			if ($platform == "czce") {
				if (strlen($variety_code) == 4) {
					$variety_code = substr($variety_code, -3);
				}
				if (strlen($slave_variety_code) == 4) {
					$slave_variety_code = substr($slave_variety_code, -3);
				}
			}

			$id = $data->id;
			$platform = $data->platform;
			// $variety_code = ($platform == "czce") ? $data->variety_code : "";
			$master_date = $data->master_date;
			$slave_platform = $data->slave_platform;
			// $slave_variety_code = $slave_variety_code;
			$slave_date = $data->slave_date;
			$wave = $data->wave;

			$monitor = array(
				"platform" => $platform,
				"variety_code" => $variety_code,
				"master_date" => $master_date,
				"slave_platform" => $slave_platform,
				"slave_variety_code" => $slave_variety_code,
				"slave_date" => $slave_date,
				"wave" => $wave,
				"expiration_date" => $expiration_date
			);

			if ($id) {
				$monitor["id"] = $id;
				array_push($update_monitor_list, $monitor);
			} else {
				$monitor["created_at"] = (new DateTime())->format('Y-m-d H:i:s');
				array_push($insert_monitor_list, $monitor);
			}
		}

		if ($insert_monitor_list) {
			$this->Monitor_variety_model->insert_batch($insert_monitor_list);
		}
		if ($update_monitor_list) {
			$this->Monitor_variety_model->update_batch($update_monitor_list);
		}

		if ($delete_ids) {
			$this->Monitor_variety_model->deleteByIds($delete_ids);
		}

		log_message("error", "handle, success.");
		$result->is_success = true;
		// 使用CodeIgniter的output类来设置JSON响应
		$this->output
			->set_content_type('application/json')
			->set_output(json_encode($result));
	}

	public function get()
	{
		$datas = $this->Monitor_variety_model->selectAll();
		$datas = $datas ? $datas : array();

		// 使用CodeIgniter的output类来设置JSON响应
    	$this->output
			->set_content_type('application/json')
			->set_output(json_encode($datas));
	}
}
